package com.guet.careapp.service.impl;

import com.google.gson.Gson;
import com.guet.careapp.controller.rest.common.errorCode.Error;
import com.guet.careapp.controller.rest.common.errorCode.ErrorCodeFactory;
import com.guet.careapp.pojo.TServiceHousekeepingInfoEntity;
import com.guet.careapp.service.IHourseKeepingService;
import com.guet.careapp.service.base.ServiceSupportImpl;
import com.guet.careapp.utils.TimeHelper;
import org.eclipse.jetty.io.EofException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class HourseKeepingServiceImpl extends ServiceSupportImpl<TServiceHousekeepingInfoEntity> implements IHourseKeepingService {

    private Logger logger = LoggerFactory.getLogger(HourseKeepingServiceImpl.class);

    @Override
    public List getAllInfos() {
        return findAll();
    }

    @Override
    public List getByKeyWords(Map map) {
        try {
//            String servant_name=map.get("servant_name")+"";
//            String phone=map.get("service_price")+"";
//            String service_name=map.get("service_name")+"";
//            String service_way=map.get("service_way")+"";
//            String service_range=map.get("service_range")+"";
//            String service_title=""+map.get("service_title");

            String keyword = map.get("keyword") + "";
            List<TServiceHousekeepingInfoEntity> list =
                    queryByHql("SELECT t FROM TServiceHousekeepingInfoEntity t WHERE t.servantName like '%" + keyword + "%' OR t.phone like '%" + keyword + "%' OR " +
                            "t.servicePrice like '%" + keyword + "%' OR t.serviceName like '%" + keyword + "%' OR t.serviceWay like '%" + keyword + "%' OR t.serviceRange like '%" + keyword + "%' OR " +
                            "t.serviceTitle like '%" + keyword + "%' OR t.serviceContent like '%" + keyword + "%'");
            return list;

        } catch (Exception e) {
            return  null;
        }


    }

    @Override
    public Object getByServiceID(String id) {

        TServiceHousekeepingInfoEntity infoEntity=findById(Integer.parseInt(id));
        if (infoEntity==null)
            return null;
        return infoEntity;

    }

    @Override
    public Error addHourseKeepingInfo(Map map) {
        String servant_name = map.get("servant_name") + "";
        String phone = map.get("service_price") + "";
        String service_name = map.get("service_name") + "";
        String service_way = map.get("service_way") + "";
        String service_range = map.get("service_range") + "";
        String service_title = "" + map.get("service_title");
        String service_content = "" + map.get(("service_content"));
        String service_price = "" + map.get("service_price");
        String img_url = "" + map.get("servant_img");
        TServiceHousekeepingInfoEntity info = new TServiceHousekeepingInfoEntity();
        info.setCreateTime(Long.parseLong(TimeHelper.getTime().toString()));
        info.setPhone(phone);
        info.setServantImg(img_url);
        info.setServantName(servant_name);
        info.setServiceContent(service_content);
        info.setServicePrice(service_price);
        info.setServiceTitle(service_title);
        info.setServiceRange(service_range);
        info.setServiceWay(service_way);
        info.setServiceName(service_name);

        try {
            save(info);
        } catch (Exception e) {
            logger.error("新增家政服务出错");
            e.printStackTrace();
            return Error.Unkonwn;
        }
        return Error.Success;
    }

    @Override
    public Error deleteHouseInfoByID(String id) {
        Integer tid = Integer.parseInt(id);
        try {
            deleteById(tid);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("删除家政信息失败");
            return Error.Unkonwn;
        }
        return Error.Success;
    }
}
